package gxnu.edu.cn.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.exception.AccountLockedException;
import com.sky.exception.AccountNotFoundException;
import com.sky.exception.PasswordErrorException;
import gxnu.edu.cn.entity.DTO.CuserLoginDTO;
import gxnu.edu.cn.mapper.CpasswordrestMapper;
import gxnu.edu.cn.mapper.CuserMapper;
import gxnu.edu.cn.entity.PasswordRest;
import gxnu.edu.cn.entity.Cuser;
import gxnu.edu.cn.service.ICuserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
@Slf4j
@Service
public class CuserServiceImpl implements ICuserService {

    @Autowired
    private CuserMapper cuserMapper;
    @Autowired
    private CpasswordrestMapper cpsaawordrestMapper;

    @Override
    public boolean sendVerificationEmail(String userName) {
        //1.查询Cuser表是否存在该用户
        Cuser user = cuserMapper.getByusername(userName);

        //2.1不存在，返回false
        if(user == null)return false;
        else {

            //2.2存在，获取邮箱，则生成验证码，发送邮件，返回true
            String email = user.getEmail();

            //生成随机的6位数字验证码
            //String verificationCode = (int)(Math.random()*1000000) + "";
            //获取截至时间

            String verificationCode = "654321";
            LocalDateTime validUntil = LocalDateTime.now().plusMinutes(5);
            PasswordRest passwordRest = new PasswordRest(userName, validUntil, verificationCode,1);
            //插入之前将以前的验证码状态改为0
            cpsaawordrestMapper.updateStatusByUserName(userName);
            cpsaawordrestMapper.insert(passwordRest);
        }
        //发送邮件




        return true;
    }


    public int validEmailCode(String userName, String verificationCode) {
        //验证cpasswordrest 表
        LocalDateTime now  = LocalDateTime.now();
        int result = cpsaawordrestMapper.validEmailCode(userName, now ,verificationCode);

        if(result == 1) cpsaawordrestMapper.updateStatusByUserName(userName);
        return result;

    }

    @Override
    public Cuser login(CuserLoginDTO cuserLoginDTO) {
        Integer userId = cuserLoginDTO.getUserID();
        String password = cuserLoginDTO.getPassword();

        //1、根据用户名查询数据库中的数据
        Cuser user = cuserMapper.getByUserIdAndPassWord(userId,password);

        //2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if (user == null) {
            //账号不存在
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }


        password= DigestUtils.md5DigestAsHex(password.getBytes());
//        log.info(password);
        if (!password.equals(user.getPassWord())) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        if (user.getAccountLocked()== StatusConstant.DISABLE) {
            //账号被锁定
            throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
        }

        //3、返回实体对象
        return user;
    }


}


